package datasources.z3950datasource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import org.jopac2.engine.Managers.JOpac2Z3950RequestManager;
import org.jopac2.engine.Z3950.SBAInternalClient;
import org.jopac2.engine.utils.SingleSearch;
import org.jopac2.jbal.RecordFactory;
import org.jopac2.jbal.RecordInterface;
import org.jopac2.utils.JOpac2Exception;
import org.jopac2.utils.RecordItem;

import com.k_int.IR.SearchTask;
import com.k_int.IR.TimeoutExceededException;


import bookhive.datasource.DataSource;
import bookhive.datastructures.datasource.DataItem;
import bookhive.datastructures.datasource.HashItem;

public class Z3950DataSource implements DataSource {
	
	JOpac2Z3950RequestManager zManager = null;
	Properties congig = null;
	
	Hashtable<String, RecordInterface> hashtable;
	
	SearchTask searchtask = null;
	String syntax = "";
	SingleSearch ss = null;
	
	public Z3950DataSource(){
		
		hashtable = new Hashtable<String, RecordInterface>();
		Properties p = new Properties();
		
		FileInputStream fis;
		try {
			File f = new File("config/z3950.properties");
			//String a = f.getAbsolutePath();
			fis = new FileInputStream(f);
			p.load(fis);
			//sol.units.it,2100,sebinaopac,JOpac2.Managers.JOpac2Z3950RequestManager,Sutrs:Sol_sutrs,a,a,0,20000
			String host = p.getProperty("host");
			int port = Integer.parseInt(p.getProperty("port"));
			String db = p.getProperty("database");
			String manager = "JOpac2.Managers.JOpac2Z3950RequestManager";
			String type = p.getProperty("type");
			syntax = type.split(":")[0].toLowerCase();
			
			try {
				ss = new SingleSearch(host, port, db, manager, type, "a", "a", "0", 20000, null, null, null);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	public Vector<DataItem> getDataResult() {
		// TODO Auto-generated method stub
		return null;
	}

	public Vector<HashItem> getHashResult() {
		// TODO Auto-generated method stub
		return null;
	}

	public HashItem getRandomRecord() {
		// TODO Auto-generated method stub
		return null;
	}

	public Vector<DataItem> sendDataQuery(String hash) {
		Vector<DataItem> ret = new Vector<DataItem>();
		DataItem di = new DataItem();
		RecordInterface ri = hashtable.get(hash);
		di.data = ri.toString();
		di.id = ri.getJOpacID();
		di.type = ri.getTipo();
		ret.add(di);
		return ret;
	}

	public Vector<HashItem> sendHashQuery(String queryData) {
		try {
			queryData = URLDecoder.decode(queryData, "UTF-8");
		} catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }
		SBAInternalClient client=new SBAInternalClient();
		try {
			searchtask=client.getJOpac2SearchData(ss, queryData, syntax);
		} catch (TimeoutExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.getRecords(ss,searchtask);
		Vector<RecordItem> records = client.getRecords();
		Vector<HashItem> ret = new Vector<HashItem>();
		long count = 0;
		Iterator<RecordItem> iterator = records.iterator();
		
		while(iterator.hasNext()){
			count++;
			RecordItem ritem = iterator.next();
			long id = count;
			if(ritem.getId()!=null) id = Long.parseLong(ritem.getId());
			else ritem.setId(String.valueOf(id));
			RecordInterface ri = RecordFactory.buildRecord(id, ritem.getData(), ritem.getDataType(), 0);
			try { ri.clearSignatures(); } catch (JOpac2Exception e) {e.printStackTrace(); }
			String hash = "";
			try { hash = ri.getHash(); } catch (Exception e) { e.printStackTrace(); }
			hashtable.put(hash, ri);
			HashItem hi = new HashItem();
			hi.hash = hash;
			hi.id = id;
			hi.type = ri.getTipo();
			if(ri.getSignatures()!=null)
				hi.addSignatures(ri.getSignatures());
			
			ret.add(hi);
		}
		
		return ret;
	}

	public boolean sendRedundQuery(String queryData) {
		return false;

	}

	public boolean sendUpdateQuery(String queryData) {
		return false;

	}

}
